package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsStorage;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin("*")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/list")
    public Object list(Integer id, String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order){
        //1. 查询并返回分页对象
        IPage<DtsCategory> resultPage = categoryService.queryCategoryPage(id,name,page,limit,sort,order);

        //获取结果集
        List<DtsCategory> list = resultPage.getRecords();
        //获取查询总条数
        long total = resultPage.getTotal();
        //封装对象返回
        Map<String,Object> data = new HashMap<>();
        data.put("total",total);
        data.put("items",list);
        return ResponseUtil.ok(data);
    }


    @PostMapping("/create")
    public Object add(@RequestBody DtsCategory category){
        DtsCategory dtsCategory = categoryService.add(category);
        return ResponseUtil.ok(dtsCategory);

    }
    @PostMapping("/update")
    public Object update(@RequestBody DtsCategory category){

        DtsCategory dtsCategory = categoryService.update(category);

        return ResponseUtil.ok(dtsCategory);
    }



    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCategory category) {

        categoryService.deleted(category);
        return ResponseUtil.ok();
    }


    @RequestMapping("/l1")
    public Object l1(){

        List<CatVo> catVoList = categoryService.categoryList();

        List<Map<String,Object>> data = catVoList.stream().map(catVo -> {
            Map<String,Object> map = new HashMap<>();
            map.put("value",catVo.getValue());
            map.put("label",catVo.getLabel());
            return map;
        }).collect(Collectors.toList());

        return ResponseUtil.ok(data);

    }

}
